/*
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "lm75.h"
#include <mc/exception.h>

using namespace dev;

bool lm75_object::init(mc::mutable_dict& csr_object, const mc::dict& connector) {
    try {
        // 验证参数
        if (csr_object.empty()) {
            MC_THROW(mc::invalid_op_exception, "csr_object is empty");
        }

        auto& manager      = dev::device_manager::get_instance();
        auto  chip_variant = manager.find_object(csr_object["object_name"].as<std::string>(),
                                                 dev::object_type::chip);
        if (std::holds_alternative<std::shared_ptr<chip_base>>(chip_variant)) {
            auto chip_ptr = std::get<std::shared_ptr<chip_base>>(chip_variant);
            if (chip_ptr) {
                m_interface_chip.m_chip = static_cast<dev::chip_base*>(chip_ptr.get());
                m_interface_chip.from_variant(csr_object);
            } else {
                MC_THROW(mc::invalid_op_exception, "chip not found, name: ${name}",
                         ("name", csr_object["object_name"].as<std::string>()));
            }
        } else {
            MC_THROW(mc::invalid_op_exception, "chip not found, name: ${name}",
                     ("name", csr_object["object_name"].as<std::string>()));
        }
    } catch (const std::exception& e) {
        elog("Lm75 init failed, exception: ${exception}", ("exception", e.what()));
        return false;
    }

    return true;
}

uint32_t lm75_object::start() {
    try {
        if (m_interface_chip.m_chip != nullptr) {
            uint32_t result = m_interface_chip.m_chip->chip_init();
            if (result == 0) {
                ilog("LM75 device started successfully");
            } else {
                elog("LM75 device start failed, error code: ${code}", ("code", result));
            }
            return result;
        } else {
            elog("LM75 device not initialized");
            return 1;
        }
    } catch (const std::exception& e) {
        elog("LM75 device start exception: ${error}", ("error", e.what()));
        return 1;
    }
}

uint32_t lm75_object::stop() {
    try {
        if (m_interface_chip.m_chip != nullptr) {
            // LM75不需要特殊的停止操作
            ilog("LM75 device stopped successfully");
            return 0;
        } else {
            elog("LM75 device not initialized");
            return 1;
        }
    } catch (const std::exception& e) {
        elog("LM75 device stop exception: ${error}", ("error", e.what()));
        return 1;
    }
}

MC_REFLECT(dev::lm75_object, ((m_interface_chip, "bmc.dev.Chip")))
